package com.colin.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URL;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        System.out.println(request.getRequestURI() + "=======");
//        System.out.println(handler + "=======");
        Object userInfo = request.getSession().getAttribute("userInfoId");
        if (request.getRequestURI().contains("/member") && userInfo == null){
            // 如果是会员中心，则需要登录
            // request.getRequestDispatcher("/public/login").forward(request , response);
            response.sendRedirect("/public/login?redirect=" + response.encodeURL(request.getRequestURI()));
            return false;
        }
        return true;
    }
}
